﻿
Imports osi.root.connector
Imports osi.root.lock
Imports osi.root.utils

Public Class pool_one_adapter(Of T, PT As idevice_pool(Of T))
    Protected ReadOnly p As PT
    Private ins_lock As slimlock.monitorlock
    Private ins As T

    Public Sub New(ByVal p As PT)
        assert(Not p Is Nothing)
        Me.p = p
    End Sub

    Protected Sub [set](ByVal ins As T)
        assert(Not ins Is Nothing)
        ins_lock.locked(Sub()
                            Me.ins = ins
                        End Sub)
    End Sub

    Protected Function [get]() As T
        Return ins
    End Function

    Protected Function drop(ByVal instance As T) As Boolean
        assert(Not instance Is Nothing)
        Return ins_lock.locked(Function()
                                   If object_compare(ins, instance) = 0 Then
                                       ins = Nothing
                                       Return True
                                   Else
                                       Return False
                                   End If
                               End Function) AndAlso
               p.release(instance)
    End Function
End Class
